package org.zfes.snowier.zbss.xzy;

import java.util.UUID;

import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zfes.snowier.common.eventpub.SimpleSubcriber;
import org.zfes.snowier.common.socket.data.MsgData;
import org.zfes.snowier.common.socket.device.group.GroupAttr;
import org.zfes.snowier.common.socket.device.group.IDeviceGroupManager;
import org.zfes.snowier.common.socket.device.sender.INettyMsgSender;
import org.zfes.snowier.common.socket.enums.TcpEventTopic;
import org.zfes.snowier.common.socket.server.chief.NettyChiefServer;
import org.zfes.snowier.web.controller.BaseController;
import org.zfes.snowier.xyz.live.IVideoStatusManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

@Controller()
@RequestMapping("/netty/test")
public class NettyTest implements  BaseController{

	@Autowired IDeviceGroupManager deviceGroupManager;
	@Autowired INettyMsgSender nettyMsgSender;
	@Autowired IVideoStatusManager videoStatusManager;
	
	 Integer timeoutSeconds=5;//多少秒超时算是没有心跳
	 Integer offineCountThreshold=2;//多少次没有心跳，视为离线
	 Integer cmdRetryCount=2;//多少次没有心跳，视为离线
	 Boolean cmdNeedAck=true;
	String groupId="main_group";
	String deviceNum="FF_505152069029";
	
	public void run() throws Exception {
		System.out.println("---------8082-------------");
		doRegist();
		init();
		NettyChiefServer.ins(8082).start();
		System.out.println("---------8082-------------");
	}

	void init() {
		deviceGroupManager.addGroupAttr(groupId, new GroupAttr(timeoutSeconds,offineCountThreshold,cmdRetryCount,cmdNeedAck) );
		deviceGroupManager.addToGroup(groupId, deviceNum);
	}
	
	public static void main(String[] args) {
		String ks="{\"0000\":{\"st\":1,\"lid\":\"sssss\"},\"0001\":{\"st\":0,\"lid\":\"\"}}";
		JSONObject liveStatus = JSON.parseObject(ks);
		System.out.println(liveStatus);
		liveStatus.entrySet().forEach(en->{
			String key=en.getKey();
			JSONObject st = JSON.parseObject(""+en.getValue());
			String lid=st.getString("lid");
			Byte status=Byte.valueOf(""+st.getInteger("st"));
			//System.out.println("chd="+key+"||status="+status+"||lid="+lid);
		});
		
	}
	
	private  void parseChannelLiveStatus(MsgData msgData) {
		String deviceNum=msgData.getDeviceNum();
		JSONObject liveStatus=(JSONObject) msgData.getData().get("rtmpS");
		//System.out.println("--liveStatus-"+liveStatus);
		liveStatus.entrySet().forEach(en->{
			String channelId=en.getKey();
			JSONObject st = JSON.parseObject(""+en.getValue());
			String liveId=st.getString("lid");
			Byte status=Byte.valueOf(""+st.getInteger("st")); 
			//System.out.println("--en-"+en);
			videoStatusManager.setLiveChannelStatus(deviceNum, channelId, liveId,status);
		});
		
		
	}
	public  void doRegist() {
		SimpleSubcriber.newIns()
		.regist(TcpEventTopic.data_receive.getValue(),val->{
			System.out.println("-onMsg-val:{"+val+"}");
		},e->{
			System.out.println("-onMsg- error："+e.getException().getMessage());
		});
		
		SimpleSubcriber.newIns()
		.regist(TcpEventTopic.data_send.getValue(),val->{
			System.out.println("-data_send-val:{"+val+"}");
		},e->{
			System.out.println("-data_send- error："+e.getException().getMessage());
		});
		
		
		SimpleSubcriber.newIns()
		.regist(TcpEventTopic.heart_beat.getValue(),val->{
			//rtmpS:{001:1,001:0}
			
			System.out.println("-heart_beat-val:{"+val+"}");
			
//			JSONObject liveStatus=(JSONObject) ((MsgData)val.getData()).getData().get("rtmpS");
			//-heart_beat-val-liveStatus:{{"0000":1,"0001":0}}
			//System.out.println("-heart_beat-val-liveStatus:{"+liveStatus+"}");
			parseChannelLiveStatus( (MsgData)val.getData());
		},e->{
			System.out.println("-heart_beat- error："+e.getException().getMessage());
		});
		
		SimpleSubcriber.newIns()
		.regist(TcpEventTopic.cmd_ack.getValue(),val->{
			System.out.println("-cmd_ack-val:{"+val+"}");
		},e->{
			System.out.println("-cmd_ack- error："+e.getException().getMessage());
		});
		
		SimpleSubcriber.newIns()
		.regist(TcpEventTopic.cmd_giveup.getValue(),val->{
			System.out.println("-cmd_giveup-val:{"+val+"}");
		},e->{
			System.out.println("-cmd_giveup- error："+e.getException().getMessage());
		});
		
		SimpleSubcriber.newIns()
		.regist(TcpEventTopic.in_acvtive.getValue(),val->{
			System.out.println("-in_acvtive-val:{"+val+"}");
		},e->{
			System.out.println("-in_acvtive- error："+e.getException().getMessage());
		});
		
		SimpleSubcriber.newIns()
		.regist(TcpEventTopic.offline.getValue(),val->{
			//System.out.println("-offline-val:{"+val+"}");
		},e->{
			System.out.println("-offline- error："+e.getException().getMessage());
		})
		;
	}
	
	
	

//    @RequestMapping(value="/send", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
//    public @ResponseBody  Object send(String val,String deviceNum){
//    	nettyMsgSender.sendToDevice(deviceNum, 
//    			MsgData.ok()
//    			.setCmdCode("000001")
//    			.setCmdDirect(Byte.valueOf("1"))
//    			.setCmdUid(UUID.randomUUID().toString())
//    			.setDeviceNum(deviceNum)
//    			.put("kkk", val));
//    	
//    	return ajaxDoneSuccess("数据操作成功 ");
//    }
    
    

}
